package com.demo.bookstore.config;

import com.alibaba.druid.filter.config.ConfigTools;
import com.alibaba.druid.util.DruidPasswordCallback;
import io.micrometer.core.instrument.util.StringUtils;

import java.util.Properties;

public class DruidPwdCallback extends DruidPasswordCallback {
    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        // 获取配置文件中的已经加密的密码（spring.datasource.druid.connect-properties.password）
        String pwd = (String)properties.get("password");
        String publicKey = (String) properties.get("config.decrypt.key");
        if (StringUtils.isNotEmpty(pwd) && StringUtils.isNotEmpty(publicKey)) {
            try {
                String password = ConfigTools.decrypt(publicKey, pwd);
                setPassword(password.toCharArray());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
